package com.base.cn.platform.os.controller.mobile.classes;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.classes.Classes;
import com.base.cn.platform.os.entity.op.coupon.OpCoupon;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5班级Controller
 *
 * @author zsw
 * @create 2018/7/5
 */
@Controller
public class MobileClassController extends MobileBaseController {

    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 加入的班级
     */
    @RequestMapping("/mobile/uc/class/user/joined")
    @ResponseBody
    public Map<String, Object> userJoinedClass(HttpServletRequest request,
                                               @RequestParam("classId") BigDecimal classId,
                                               @RequestParam("requestUrl") String requestUrl) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> classes = classesService.findClassesById(classId, false, false, false, false, 0);
        Integer classesType = (Integer) classes.get("classesType");
        if (classesType == 1){
            Object courseId = classes.get("courseId");
            HashMap<String, Object> map = new HashMap<>();
            map.put("courseId",courseId);
            map.put("classesUrl",requestUrl);
            customCacheChannel.set(J2CacheRegion.THIRD_MINUTE_REGION.getKey(),CacheConstants.USER_BUY_COURSE_JOIN_CLASS+userId.toString(),map);
        }
        return classesMemberService.saveClassesMemberBatch(classId, userId.toString(), 1, 1);
    }

    /**
     * 班级搜索
     *
     * @param request
     * @param name 班级名称
     * @return
     */
    @RequestMapping("/mobile/class/search")
    public String toSearchResult(HttpServletRequest request,
                                 @RequestParam("name") String name) {
        request.setAttribute("name", name);
        return resultPage("/class/class-search");
    }

    /**
     * 班级搜索结果
     *
     * @param classes 搜索条件
     */
    @RequestMapping("/mobile/class/search/ajax/result")
    public String listSearchResult(Model model,
                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                   @RequestBody Map<String, Object> classes) {
        classes.put("status",1);
        Map<String, Object> classesListPage = classesService.findClassesListPage(classes, currentPage, 10, false, false, false, false);
        model.addAttribute("dataMap",classesListPage);
        return resultPage("/class/index/ajax-list-class");
    }


    /**
     * 获取关联班级
     * @param courseId 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/mobile/class/getCourseRelevanceClasses")
    @ResponseBody
    public Map<String,Object> getCourseRelevanceClasses(@RequestParam( value =  "courseId") BigDecimal courseId){

        List<Map> courseRelevanceClasses = classesService.getCourseRelevanceClasses(courseId);
        return ResultUtil.SUCCESS(courseRelevanceClasses);
    }

}
